<?php
namespace Qy {
    /**
    * 企业微信客服服务类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%AE%A2%E6%9C%8D%E6%9C%8D%E5%8A%A1
    */
    class Kf {
        private $common;
        
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 向客服人员发消息 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%AE%A2%E6%9C%8D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function sendKf($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['sender']) || !isset($ary['receiver'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/kf/send?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 获取客服列表
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%AE%A2%E6%9C%8D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $type
        * @param AccessToken $a_token
        */
        public function getKflist($type=0 ,$a_token = '') {
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            switch ($type) {
                case 1:
                    $url = "https://qyapi.weixin.qq.com/cgi-bin/kf/list?access_token={$a_token}&type=internal";
                    break;
                case 2:
                    $url = "https://qyapi.weixin.qq.com/cgi-bin/kf/list?access_token={$a_token}&type=external";
                    break;
                default:
                    $url = "https://qyapi.weixin.qq.com/cgi-bin/kf/list?access_token={$a_token}";
                    break;
            }
            return $this->common->httpRequest($url);
        }
    }
}
?>
